# -*- encoding=utf8 -*-
__author__ = "v_jwjwchen"
content = "在线充值摩拜 美团单车月卡优惠购卡（秒到）\r\n 1元就能购买月卡优惠券，一直在线秒发货秒到！！不用问在吗，链接在就有货,直接拍下后,点击我想要,发手机号码给我即可。\r\n拍下后，发号码给我，一定是绑定摩拜账号的手机号喔，我会马上发货\r\n         注意有两种充值方式：\r\n        第一种到账方式：直接充值到账一个月的月卡，可以直接使用美团app直接扫码骑车，非常爽\r\n         第二种到账方式：到账一张20元的月卡优惠券，你可以用这张优惠券去直接购买摩拜月卡，非常划算，折下来月卡只需5元\月\r\n       以上两种充值方式是随机的，不可选的，主要看你运气了，1块钱你买不了吃亏，1块钱你也买不到上当，1块钱却可以让你以非常优惠的价格购买月卡，何乐而不为呢"
from airtest.core.api import *
# from airtest.core.api import connect_device
# dev = connect_device('Android://127.0.0.1:5037/emulator-5554')
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
x,y = poco.get_screen_size() #获取屏幕分辨率
#-----------------------------------------------------
def wait_for_appearance_and_click(object):
    object.wait_for_appearance()
    object.click()
        
def match_object_and_click(match_value,attr_type,object_list):
    '''
    match_value:匹配的值
    object_list:被匹配的poco元素对象列表
    attr_type:匹配的类型值
    '''
    count = len(object_list)
    for i in range(count):
        text_value = object_list[i].attr("text")
        if text_value:
            if match_value in text_value:
                print (object_list[i],text_value)
                object_list[i].click()
            
def click_element_list(object_list,click_max_times):
    '''
    object_list:被匹配的poco元素对象列表
    click_max_times:最多点击次数
    '''
    a=0
    count = len(object_list)
    for i in range(count):
        j=abs(i-count+1)
        object_list[j].click()
        a+=1
        if a == click_max_times:
            break
        
#----------------------------------
# match_object_and_click("发布闲置","text",poco(name="android.widget.ImageView",type="android.widget.ImageView"))

# poco(textMatches='^发布闲置[\s\S]*$').click()

#-----------------------------------------------------    
package = "com.taobao.idlefish"
stop_app(package)
sleep(5)
start_app(package)
#-----------------------------------------------------
while True:
    wait_for_appearance_and_click(poco(nameMatches='^我的.*$',descMatches='^我的.*$',touchable=True))
    wait_for_appearance_and_click(poco(descMatches='^卖闲置.*$',touchable=True))
    wait_for_appearance_and_click(poco(textMatches='^发布闲置[\s\S]*$'))
    sleep(5)
    click_element_list(poco(text='未选中',touchable=True),click_max_times=5)
    wait_for_appearance_and_click(poco(textMatches='^下一步.*$',touchable=True))
    sleep(5)
    wait_for_appearance_and_click(poco(textMatches='^下一步.*$',touchable=True))
#     wait_for_appearance_and_click(poco(textMatches='^品牌型号.*$',touchable=True))
    wait_for_appearance_and_click(poco(name='android.widget.EditText',touchable=True))
    text(content)
    wait_for_appearance_and_click(poco(textMatches='^价格[\s\S]*$'))
    wait_for_appearance_and_click(poco(text='1',touchable=True))
#     wait_for_appearance_and_click(poco(text='.',touchable=True))
#     wait_for_appearance_and_click(poco(text='8',touchable=True))
    wait_for_appearance_and_click(poco(text='包邮',touchable=True))
    wait_for_appearance_and_click(poco(text='确定',touchable=True))
    wait_for_appearance_and_click(poco(text='发布',touchable=True))
    keyevent("BACK")
    sleep(60)